﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace mrp16062010.Veri
{
    class Siparis:ConnectionImporter
    {
        private int siparisId;

        public int SiparisId
        {
            get { return siparisId; }
            set { siparisId = value; }
        }
        private Stok stok;

        public Stok _Stok
        {
            get { return stok; }
            set { stok = value; }
        }

        private int miktar;

        public int Miktar
        {
            get { return miktar; }
            set { miktar = value; }
        }

        private DateTime teslimTarihi;

        public DateTime TeslimTarihi
        {
            get { return teslimTarihi; }
            set { teslimTarihi = value; }
        }

        private DateTime siparisTarihi;

        public DateTime SiparisTarihi
        {
            get { return siparisTarihi; }
            set { siparisTarihi = value; }
        }

        private int oncelik;

        public int Oncelik
        {
            get { return oncelik; }
            set { oncelik = value; }
        }


        public void Kaydet()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("InsertSiparis", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmReturnValue = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            prmReturnValue.Direction = ParameterDirection.ReturnValue;

            #region Populate Parameters

            SqlParameter prmStokId = cmd.Parameters.Add("@StokId", SqlDbType.Int);
            prmStokId.Direction = ParameterDirection.Input;

            prmStokId.Value = this._Stok.StokId;


            SqlParameter prmMiktar = cmd.Parameters.Add("@Miktar", SqlDbType.Int);
            prmMiktar.Direction = ParameterDirection.Input;

            prmMiktar.Value = this.Miktar;


            SqlParameter prmTeslimTarihi = cmd.Parameters.Add("@TeslimTarihi", SqlDbType.DateTime);
            prmTeslimTarihi.Direction = ParameterDirection.Input;

            prmTeslimTarihi.Value = this.TeslimTarihi;


            SqlParameter prmSiparisTarihi = cmd.Parameters.Add("@SiparisTarihi", SqlDbType.DateTime);
            prmSiparisTarihi.Direction = ParameterDirection.Input;

            prmSiparisTarihi.Value = this.SiparisTarihi;


            SqlParameter prmOncelik = cmd.Parameters.Add("@Oncelik", SqlDbType.Int);
            prmOncelik.Direction = ParameterDirection.Input;

            prmOncelik.Value = this.Oncelik;


            SqlParameter prmSiparisId = cmd.Parameters.Add("@SiparisId", SqlDbType.Int);


            prmSiparisId.Direction = ParameterDirection.Output;



            #endregion


            cmd.ExecuteNonQuery();
            this.SiparisId = int.Parse(prmSiparisId.Value.ToString());
            cmd.Dispose();
            Disconnect();
        }

        public void Sil()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("DeleteSiparis", Connection);
            cmd.CommandType = CommandType.StoredProcedure; 
            SqlParameter prmSiparisId = cmd.Parameters.Add("@SiparisId", SqlDbType.Int);
            prmSiparisId.Direction = ParameterDirection.Input;
            prmSiparisId.Value = this.siparisId;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Guncelle()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("UpdateSiparis", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmSiparisId = cmd.Parameters.Add("@SiparisId", SqlDbType.Int);
            prmSiparisId.Direction = ParameterDirection.Input;

            prmSiparisId.Value = this.SiparisId;


            SqlParameter prmStokId = cmd.Parameters.Add("@StokId", SqlDbType.Int);
            prmStokId.Direction = ParameterDirection.Input;

            prmStokId.Value = this.stok.StokId;


            SqlParameter prmMiktar = cmd.Parameters.Add("@Miktar", SqlDbType.Int);
            prmMiktar.Direction = ParameterDirection.Input;

            prmMiktar.Value = this.Miktar;


            SqlParameter prmTeslimTarihi = cmd.Parameters.Add("@TeslimTarihi", SqlDbType.DateTime);
            prmTeslimTarihi.Direction = ParameterDirection.Input;

            prmTeslimTarihi.Value = this.TeslimTarihi;


            SqlParameter prmSiparisTarihi = cmd.Parameters.Add("@SiparisTarihi", SqlDbType.DateTime);
            prmSiparisTarihi.Direction = ParameterDirection.Input;

            prmSiparisTarihi.Value = this.SiparisTarihi;


            SqlParameter prmOncelik = cmd.Parameters.Add("@Oncelik", SqlDbType.Int);
            prmOncelik.Direction = ParameterDirection.Input;

            prmOncelik.Value = this.Oncelik;


            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Bul()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectSiparis", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmSiparisId = cmd.Parameters.Add("@SiparisId", SqlDbType.Int);
            prmSiparisId.Direction = ParameterDirection.Input;

            prmSiparisId.Value = this.SiparisId;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                this.siparisId = int.Parse(row["SiparisId"].ToString());
                this.stok.StokId = int.Parse(row["StokId"].ToString());
                this.miktar = int.Parse(row["Miktar"].ToString());
                this.teslimTarihi = DateTime.Parse(row["TeslimTarihi"].ToString());
                this.siparisTarihi = DateTime.Parse(row["SiparisTarihi"].ToString());
                this.oncelik = int.Parse(row["Oncelik"].ToString());
                break;
            }

            cmd.Dispose();
            Disconnect();
        }

        public List<Siparis> Listele()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectRotaTanimsAll", Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<Siparis> liste = new List<Siparis>();
            foreach (DataRow row in dt.Rows)
            {
                Siparis yeniSiparis = new Siparis();
                yeniSiparis.siparisId = int.Parse(row["SiparisId"].ToString());
                yeniSiparis.stok.StokId = int.Parse(row["StokId"].ToString());
                yeniSiparis.miktar = int.Parse(row["Miktar"].ToString());
                yeniSiparis.teslimTarihi = DateTime.Parse(row["TeslimTarihi"].ToString());
                yeniSiparis.siparisTarihi = DateTime.Parse(row["SiparisTarihi"].ToString());
                yeniSiparis.oncelik = int.Parse(row["Oncelik"].ToString());
                liste.Add(yeniSiparis);
            }

            cmd.Dispose();
            Disconnect();
            return liste;
        }
        
        
    }
}
